package com.cetc39.dao.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cetc39.bean.FirstPlan;
import com.cetc39.bean.Node;
import com.cetc39.dao.FirstplanDao;

public class FirstplanDaoImpl extends HibernateDaoSupport implements FirstplanDao {

	@Override
	public void save(FirstPlan fp) {
		this.getHibernateTemplate().save(fp);
	}

	@Override
	public void delete(int id) {
		FirstPlan fp = this.getHibernateTemplate().get(FirstPlan.class, id);
		this.getHibernateTemplate().delete(fp);
	}

	@Override
	public void update(FirstPlan fp) {
		this.getHibernateTemplate().saveOrUpdate(fp);
	}

	@Override
	public List<?> findAll() {
		List<?> fpList = this.getHibernateTemplate().find("FROM FirstPlan");
		return fpList;
	}

	@Override
	public FirstPlan findById(int id) {
		FirstPlan fp = this.getHibernateTemplate().get(FirstPlan.class, id);
		return fp;
	}

	@Override
	public List<?> findByProjectId(int id) {
		List<?> firstplanList = this.getHibernateTemplate().find("FROM FirstPlan WHERE project.id=?", new Object[]{id});
		return firstplanList;
	}

	@Override
	public void release(int plan_id) {
		FirstPlan fp = this.getHibernateTemplate().get(FirstPlan.class, plan_id);
		fp.setPlan_release(2);
		this.getHibernateTemplate().saveOrUpdate(fp);
	}

	@Override
	public List<?> findReleasedPlan() {
		return this.getHibernateTemplate().find("FROM FirstPlan WHERE plan_release=1");
	}

	@Override
	public List<?> findReleasedPlanByCondition(String[] conditions) {
		StringBuffer queryString = new StringBuffer();
		queryString.append("FROM FirstPlan WHERE plan_release=1");
		if(conditions[0] != null && !"".equals(conditions[0])) {
			queryString.append(" AND project.leader_39.id=" + conditions[0]);
		}
		if(conditions[1] != null && !"".equals(conditions[1])) {
			queryString.append(" AND project.head_dept.id=" + conditions[1]);
		}
		if(conditions[2] != null && !"".equals(conditions[2])) {
			queryString.append(" AND project.project_filed.id=" + conditions[2]);
		}
		if(conditions[3] != null && !"".equals(conditions[3])) {
			queryString.append(" AND project.project_status=" + conditions[3]);
		}
		if(conditions[4] != null && !"".equals(conditions[4])) {
			queryString.append(" AND project.time_details='" + conditions[4] + "'");
		}
		if(conditions[5] != null && !"".equals(conditions[5])) {
			queryString.append(" AND project.client.id=" + conditions[5]);
		}
		if(conditions[6] != null && !"".equals(conditions[6])) {
			queryString.append(" AND project.project_code=" + conditions[6]);
		}
		System.out.println(queryString);
		return this.getHibernateTemplate().find(queryString.toString());
	}

	@Override
	public void access_plan(int plan_id) {
		FirstPlan fp = this.getHibernateTemplate().get(FirstPlan.class, plan_id);
		fp.setPlan_release(1);
		this.getHibernateTemplate().saveOrUpdate(fp);
	}

	@Override
	public void reject_plan(int plan_id) {
		FirstPlan fp = this.getHibernateTemplate().get(FirstPlan.class, plan_id);
		fp.setPlan_release(0);
		this.getHibernateTemplate().saveOrUpdate(fp);
	}

	@Override
	public List<?> findReviewdPlan() {
		List<?> nodeList = this.getHibernateTemplate().find("FROM Node WHERE status=2");
		Set<FirstPlan> firstplanSet = new HashSet<FirstPlan>();
		List<FirstPlan> firstplanList = new ArrayList<FirstPlan>();
		for(int i=0; i<nodeList.size(); i++) {
			Node n = (Node) nodeList.get(i);
			firstplanSet.add(n.getFirstplan());
		}
		for(FirstPlan fp : firstplanSet) {
			firstplanList.add(fp);
		}
		return firstplanList;
	}


}
